package com.caojing.syby.ui.brandsale.brandchilddetails;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.caojing.syby.R;
import com.caojing.syby.base.BaseActivity;
import com.caojing.syby.bean.home.HomeGvBean;
import com.caojing.syby.bean.home.RowsBean;
import com.caojing.syby.ui.adapter.HomeGvAdapter;
import com.caojing.syby.ui.details.productdetail.ClickGridviewItemActivity;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class BrandChildDetailsActivity extends BaseActivity implements BrandChildDetailsContract.View {


    @Bind(R.id.iv_brand_details_logo)
    ImageView ivBrandDetailsLogo;
    @Bind(R.id.tv_brand_details_name)
    TextView tvBrandDetailsName;
    @Bind(R.id.tv_brand_details_discount)
    TextView tvBrandDetailsDiscount;
    @Bind(R.id.iv_brand_details_ceshibai1)
    ImageView ivBrandDetailsCeshibai1;
    @Bind(R.id.gv_pull_brand_details)
    PullToRefreshGridView gvPullBrandDetails;

    private List<RowsBean> totalList;
    private  int PAGES=1;
    private BrandChildDetailsContract.Presenter persenter;
    private HomeGvAdapter homeGvAdapter;
    private GridView gridView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_brand_child_details;
    }

    @Override
    protected void initView() {
        //获得点击事件传来的值
        Intent intent = getIntent();
        Glide.with(this).load(intent.getStringExtra("url")).into(ivBrandDetailsLogo);
        tvBrandDetailsName.setText(intent.getStringExtra("xiu"));
        tvBrandDetailsDiscount.setText(intent.getStringExtra("zhe"));

        persenter=new BrandChildDetailsPersenter(this);
        persenter.initBrandDetailsData(initParams());

        //没有数据是显示该图片
        gvPullBrandDetails.setEmptyView(ivBrandDetailsCeshibai1);
        //得到控件
        gridView = gvPullBrandDetails.getRefreshableView();

    }

    private HashMap<String, String> initParams() {

        HashMap<String,String> map = new HashMap<>();
        map.put("act", "getworth");
        map.put("pages", PAGES+"");
        map.put("bc", "0");
        map.put("brandid", getIntent().getStringExtra("brandid"));
        map.put("v", "34");
        return map;
    }


    @Override
    protected void initData() {
        totalList=new ArrayList<>();
        homeGvAdapter = new HomeGvAdapter(totalList,BrandChildDetailsActivity.this);
        gridView.setAdapter(homeGvAdapter);

        gvPullBrandDetails.setMode(PullToRefreshBase.Mode.BOTH);
        gvPullBrandDetails.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                gvPullBrandDetails.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                PAGES++;
                persenter.initBrandDetailsData(initParams());
            }
        });

    }


    @Override
    public void onBrandDetailsSuccess(HomeGvBean homeGvBean) {
        List<RowsBean> rowsBeen = homeGvBean.getRows();
        totalList.addAll(rowsBeen);
        homeGvAdapter.notifyDataSetChanged();
        gvPullBrandDetails.onRefreshComplete();
        //点击跳转到ClickGridViewActivity（webView）
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BrandChildDetailsActivity.this, ClickGridviewItemActivity.class);
                RowsBean rowsBean = totalList.get(position);
                if (rowsBean != null) {
                    String spreadUrl = rowsBean.getSpreadUrl();
                    intent.putExtra("spreadUrl", spreadUrl);
                    String name = rowsBean.getName();
                    intent.putExtra("name",name);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onBrandDetailsDataFail(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
